package com.wm.system.generator;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Collections;

public class GeneratorCode {

    public static void main(String[] args) {
        testGenerator();
    }

    public static void testGenerator() {
        String projectPath = System.getProperty("user.dir");
        // 数据源配置
        DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig
                .Builder(
                "jdbc:mysql://localhost:3306/second_hand_car?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8",
                "root",
                "123456")
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());

        FastAutoGenerator.create(dataSourceConfig)
                .globalConfig(builder -> {
                    builder.author("hyq") //设置作者
                            .commentDate("yyyy-MM-dd HH:mm:ss")//注释日期
                            .enableSwagger() //开启swagger
                            .outputDir(projectPath + "\\wm-system\\src\\main\\java"); //指定输出目录

                })
                .packageConfig(builder -> {
                    builder.parent("com.wm")// 设置父包名
                           .moduleName("system")
                            .entity("domain")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,projectPath + "\\wm-system\\src\\main\\resources\\mapper\\system"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude("t_city") // 设置需要生成的表名
//                            .addTablePrefix("t_")  // 设置过滤表前缀
//                            .serviceBuilder() //service策略配置
//                            .formatServiceFileName("%Service")
//                            .formatServiceImplFileName("%Service")
                            .entityBuilder() //实体类策略配置
                            .enableLombok() //开启 lombok 模型
                            .enableTableFieldAnnotation() //开启生成实体时生成字段注解
                            .controllerBuilder()
                            .enableRestStyle();//开启生成@RestController 控制器
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
